package com.gopay.common.util.cbrmb;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.hibernate.Hibernate;
import org.hibernate.type.Type;

@SuppressWarnings("deprecation")
public class HibernateScalarQueryUtils {
	private static final Map<java.lang.reflect.Type, org.hibernate.type.Type> typeMapping;
	
	static {
		typeMapping = new HashMap<>();
		typeMapping.put(String.class, Hibernate.STRING);
		typeMapping.put(Integer.class, Hibernate.INTEGER);
		typeMapping.put(Long.class, Hibernate.LONG);
		typeMapping.put(Date.class, Hibernate.DATE);
		typeMapping.put(BigDecimal.class, Hibernate.BIG_DECIMAL);
		typeMapping.put(Character.class, Hibernate.CHARACTER);
		typeMapping.put(Byte.class, Hibernate.BYTE);
		typeMapping.put(Boolean.class, Hibernate.BOOLEAN);
	}
	
	public static Map<String, Type> getScalarMap(Class<?> beanClass) {
		Field[] fields = beanClass.getDeclaredFields();
		if (fields.length == 0) {
			return null;
		}
		Map<String, Type> scalarMap = new HashMap<>();
		for (int i = 0; i < fields.length; i++) {
			if (Modifier.PRIVATE == fields[i].getModifiers()) {
				scalarMap.put(fields[i].getName(), typeMapping.get(fields[i].getType()));
			}
		}
		return scalarMap;
	}
	
}
